package cn.itsource.system.service.impl;

import cn.itsource.org.domain.Department;
import cn.itsource.system.domain.Menu;
import cn.itsource.system.mapper.MenuMapper;
import cn.itsource.system.service.IMenuService;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2022-08-14
 */
@Service
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements IMenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> getTree() {
        List<Menu> all = menuMapper.loadAll();//获取所有部门
        List<Menu> result = new ArrayList<>();//声明返回集合 装一级部门
        for (Menu menu: all) {
            if(menu.getParentId() == null){//一级部门
                result.add(menu);
            }else{//再遍历所有部门 找父级部门
                for (Menu menu1 : all) {
                    if(menu1.getId() == menu.getParentId()){
                        List<Menu> children = menu1.getChildren();
                        children.add(menu);
                    }
                }
            }
        }
        return result;
    }
}
